package com.unlcn.ils.crsc.api.controller;

import com.unlcn.ils.crsc.api.vo.RestVO;
import com.unlcn.ils.crsc.dao.model.crsc.CrscOtd;
import com.unlcn.ils.crsc.service.annotation.QueryLogRecorder;
import com.unlcn.ils.crsc.service.crsc.CrscOTDService;
import com.unlcn.ils.crsc.service.util.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/otd")
@Api(value = "/otd", description = "中铁在途接口", tags = {"中铁在途接口"})
public class CrscOtdController {

    private static final Logger logger = LoggerFactory.getLogger(CrscOtdController.class);

    @Autowired
    private CrscOTDService crscOTDService;

    @GetMapping("/getList")
    @ApiOperation(value = "根据VIN查询最新的在途信息", notes = "根据VIN查询最新的在途信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestVO.class)
    @QueryLogRecorder("查询拦截")
    public RestVO<List<CrscOtd>> getListByVin(@RequestParam("vinStrs") String vinStrs) {
        RestVO<List<CrscOtd>> result = new RestVO<>(0, null, "success");
        try {
            List<CrscOtd> listCrscOtds = crscOTDService.getOtdsByVin(vinStrs);
            result.setData(listCrscOtds);
        } catch (Exception e) {
            logger.error("CrscOtdController.getListByVin ERROR Message : {} ", e.getMessage());
            result.setCode(500);
            result.setMessage("查询错误");
        }
        return result;
    }

    @PostMapping("/queryVinDetail")
    @ApiOperation(value = "根据VIN查询最新的在途信息-POST", notes = "根据VIN查询最新的在途信息-POST", produces = MediaType.APPLICATION_JSON_VALUE, response = RestVO.class)
    @QueryLogRecorder("查询拦截")
    public RestVO<List<CrscOtd>> queryVinDetail(@RequestParam("vinStrs") String vinStrs) {
        RestVO<List<CrscOtd>> result = new RestVO<>(0, null, "success");
        try {
            List<CrscOtd> listCrscOtds = crscOTDService.queryVinDetail(vinStrs);
            result.setData(listCrscOtds);
        } catch (BaseException be) {
            logger.error("CrscOtdController.getListByVin BaseException : {} ", be);
            result.setCode(-1);
            result.setMessage(be.getMessage());
        } catch (Exception e) {
            logger.error("CrscOtdController.getListByVin ERROR: {} ", e.getMessage());
            result.setCode(-1);
            result.setMessage("查询错误");
        }
        return result;
    }

    @GetMapping("/queryDate")
    @ApiOperation(value = "手动拉取订单在途", notes = "手动拉取订单在途", produces = MediaType.APPLICATION_JSON_VALUE, response = RestVO.class)
    public RestVO<Boolean> queryDate(@RequestParam("queryDate") String queryDate) {
        RestVO<Boolean> result = new RestVO<>(0, null, "success");
        Boolean blnRtn = true;
        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = format1.parse(queryDate);
            crscOTDService.readFTPFilesToSaveDB(date);
            result.setData(blnRtn);
        } catch (Exception e) {
            logger.error("CrscOtdController.queryDate ERROR Message : {} ", e.getMessage());
            result.setCode(500);
            result.setMessage("查询错误");
            result.setData(false);
        }
        return result;
    }


}
